#include "videographicview.h"

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
QLabelWindow::QLabelWindow(QWidget* parent)
    : QWidget(parent), m_label(new QLabel("", parent))
{
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    m_label->setStyleSheet("background-color: rgba(0, 0, 0, 0); color: white; font-size: 16px;");
    m_label->setAlignment(Qt::AlignCenter);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(m_label);
    setLayout(layout);
    //setFixedSize(300, 100);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void QLabelWindow::UpdateLabelText(const std::string& text)
{
    m_label->setText(QString::fromStdString(text));
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void QLabelWindow::UpdatePosition(const QPoint& point)
{
    move(point);
}

////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
ParentEventFilter::ParentEventFilter(QLabelWindow* labelWindow,
                                     QWidget* parent)
    : QObject(parent), m_graphics_view(parent), m_labelWindow(labelWindow)
{
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool ParentEventFilter::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::Move) {
        QPoint globalPos = m_graphics_view->mapToGlobal(QPoint(10, 10));
        m_labelWindow->UpdatePosition(globalPos);
    }
    return QObject::eventFilter(obj, event);
}

////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GraphicsView::GraphicsView(const std::string& render_id,
                           IRenderHandler* handler,
                           QWidget* parent)
    : QGraphicsView(parent)
    , m_scene(new QGraphicsScene(this))
    , m_labelWindow(new QLabelWindow(this))
    , m_handler(handler)
    , m_render_id(render_id)
{
    setScene(m_scene);
    setBackgroundBrush(Qt::black);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    setAttribute(Qt::WA_PaintOnScreen,true);
    setUpdatesEnabled(false);

    // 初始化 QLabel window 位置
    UpdateLabelWindowPosition();
    m_labelWindow->show();

    // Install event filter on the parent window
    QWidget* main_wnd = (QWidget*)parent->parent()->parent();
    ParentEventFilter* filter = new ParentEventFilter(m_labelWindow, this);
    main_wnd->installEventFilter(filter);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GraphicsView::~GraphicsView()
{
    delete m_labelWindow;
    delete m_scene;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void GraphicsView::UpdateLabelWindowPosition()
{
    // 更新 QLabel window 位置以跟随 QGraphicsView
    m_labelWindow->UpdatePosition(mapToGlobal(QPoint(10, 10)));
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void GraphicsView::UpdateLabelText(const std::string& text)
{
    m_labelWindow->UpdateLabelText(text);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void GraphicsView::SetGeometry(int ax, int ay, int aw, int ah)
{
    QGraphicsView::setGeometry(ax, ay, aw, ah);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void* GraphicsView::WinId()
{
    return (void*)QGraphicsView::winId();
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void GraphicsView::paintEvent(QPaintEvent*)
{
    if (m_handler) {
        m_handler->OnPaint(m_render_id);
    }
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void GraphicsView::Show()
{
    QGraphicsView::show();
}

////////////////////////////////////////////////////////////////////////////////

WidgetView::WidgetView(const std::string& render_id,
                       IRenderHandler* handler,
                       QWidget* parent)
    : QWidget(parent)
    , m_handler(handler)
    , m_render_id(render_id)
{
    setAttribute(Qt::WA_OpaquePaintEvent); // 禁用 Qt 自动填充背景
    setAttribute(Qt::WA_NoSystemBackground); // 禁用系统背景
    setAttribute(Qt::WA_NativeWindow);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::UpdateLabelText(const std::string& text)
{

}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::SetGeometry(int ax, int ay, int aw, int ah)
{
    QWidget::setGeometry(ax, ay, aw, ah);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void* WidgetView::WinId()
{
    return (void*)QWidget::winId();
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::Show()
{
    QWidget::show();
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::paintEvent(QPaintEvent* event)
{
    qDebug() << "paintEvent";

    if (m_handler) {
        m_handler->OnPaint(m_render_id);
    }
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::resizeEvent(QResizeEvent *event)
{
    qDebug() << "resizeEvent";

    if (m_handler) {
        m_handler->OnSizeChanged(m_render_id);
    }
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void WidgetView::moveEvent(QMoveEvent *event)
{
    qDebug() << "moveEvent";

    if (m_handler) {
        m_handler->OnPaint(m_render_id);
    }
}

#include <Windows.h>
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool WidgetView::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
{
    MSG* msg = static_cast<MSG*>(message);
    if (msg->message == WM_MOVING) {
        qDebug() << "WM_MOVING";
    } else if (msg->message == WM_ERASEBKGND) {
        qDebug() << "WM_ERASEBKGND";
        if (m_handler) {
            m_handler->OnPaint(m_render_id);
        }
        *result = 1;
        return true;
    }
    return QWidget::nativeEvent(eventType, message, result);
}
